package com.fit.spring.boot.mail;


import com.fit.spring.boot.mail.service.MailService;
import com.fit.spring.boot.mail.service.impl.MailServiceImpl;
import org.apache.velocity.VelocityContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class App {


//配置插件模式 自动扫描 MailServiceImpl
    public static void main(String[] args) {

        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
        System.out.println("启动完成");

        MailService mailService = app.getBean(MailService.class);
        sendFile(mailService);

    }



    /**
     * @describe: 发送邮件，通过输入流的方式
     * @author: hcl
     * @date: 2019/3/13 0:27
     * @param: [mailService]
     * @return void
     */
    public static void sendInputStream(MailService mailService){

        //模板
        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"en\" xmlns:th=\"http://www.thymeleaf.org\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\" />\n" +
                "    <title>邮件模板</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h2>你好！看见请回复！</h2>\n" +
                "    <p>姓名 <span >${name}</span></p><br/>\n" +
                "    <p>年龄 <span >${age}</span></p><br/>\n" +
                "    <p>性别 <span >${sex}</span></p><br/>\n" +
                "    <img src=\"$gif\"/><br/>\n" +
                "\t#foreach($img in $images)\n" +
                "    <img src=\"$img\" /><br/>\n" +
                "    #end\n" +
                "\t\n" +
                "</body>\n" +
                "</html>\n";

        List<String> targetEail = new ArrayList<>();
        targetEail.add("1773123249@qq.com");

        List<String> ccPeoples = new ArrayList<>();
//        ccPeoples.add("hechunlin1993@outlook.com");

        List<String> bccPeoples = new ArrayList<>();
//        bccPeoples.add("13668125517@139.com");

        VelocityContext context = new VelocityContext();
        context.put("name","张三");
        context.put("age","30");
        context.put("sex","男");

        List<MailServiceImpl.ImageResource> imageResources = new ArrayList<>();
        List<String> imageNames = new ArrayList<>();
        try {
            //创建图片资源(图片集合)
            MailServiceImpl.ImageResource imageResource1 = new MailServiceImpl.ImageResource("1.png",new FileInputStream("E:/1.png"));
            imageNames.add(imageResource1.getName());
            imageResources.add(imageResource1);
            MailServiceImpl.ImageResource imageResource2 = new MailServiceImpl.ImageResource("1.jpg",new FileInputStream("E:/1.jpg"));
            imageNames.add(imageResource2.getName());
            imageResources.add(imageResource2);
            context.put("images",imageNames);

            //创建图片资源
            MailServiceImpl.ImageResource imageResource3 = new MailServiceImpl.ImageResource("动图.gif",new FileInputStream("E:/动图.gif"));
            context.put("gif",imageResource3.getName());
            imageResources.add(imageResource3);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //添加附件
        List<MailServiceImpl.AttachmentResource> attachments = new ArrayList<>();

        try {
            attachments.add(new MailServiceImpl.AttachmentResource("任务.zip",new FileInputStream("E:/任务.zip")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mailService.sendHtmlTemplateMail("邮件测试3",targetEail, ccPeoples, bccPeoples,attachments,html,context,imageResources);
    }

    /**
     * @describe: 发送邮件，通过文件的方式
     * @author: hcl
     * @date: 2019/3/13 0:27
     * @param: [mailService]
     * @return void
     */
    public static void sendFile(MailService mailService){

        //模板
        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"en\" xmlns:th=\"http://www.thymeleaf.org\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\" />\n" +
                "    <title>邮件模板</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h2>你好！看见请回复！</h2>\n" +
                "    <p>姓名 <span >${name}</span></p><br/>\n" +
                "    <p>年龄 <span >${age}</span></p><br/>\n" +
                "    <p>性别 <span >${sex}</span></p><br/>\n" +
                "    <img src=\"$gif\"/><br/>\n" +
                "\t#foreach($img in $images)\n" +
                "    <img src=\"$img\" /><br/>\n" +
                "    #end\n" +
                "\t\n" +
                "</body>\n" +
                "</html>\n";

        List<String> targetEail = new ArrayList<>();
        targetEail.add("1773123249@qq.com");

        List<String> ccPeoples = new ArrayList<>();
//        ccPeoples.add("hechunlin1993@outlook.com");

        List<String> bccPeoples = new ArrayList<>();
//        bccPeoples.add("13668125517@139.com");

        VelocityContext context = new VelocityContext();
        context.put("name","张三");
        context.put("age","30");
        context.put("sex","男");

        List<MailServiceImpl.ImageResource> imageResources = new ArrayList<>();
        List<String> imageNames = new ArrayList<>();
        try {
            //创建图片资源(图片集合)
            MailServiceImpl.ImageResource imageResource1 = new MailServiceImpl.ImageResource(new File("E:/1.png"));
            imageNames.add(imageResource1.getName());
            imageResources.add(imageResource1);
            MailServiceImpl.ImageResource imageResource2 = new MailServiceImpl.ImageResource(new File("E:/1.jpg"));
            imageNames.add(imageResource2.getName());
            imageResources.add(imageResource2);
            context.put("images",imageNames);

            //创建图片资源
            MailServiceImpl.ImageResource imageResource3 = new MailServiceImpl.ImageResource(new File("E:/动图.gif"));
            context.put("gif",imageResource3.getName());
            imageResources.add(imageResource3);

        } catch (Exception e) {
            e.printStackTrace();
        }

        //添加附件
        List<MailServiceImpl.AttachmentResource> attachments = new ArrayList<>();

        try {
            attachments.add(new MailServiceImpl.AttachmentResource("做任务.jpg",new File("E:/任务.zip")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        mailService.sendHtmlTemplateMail("邮件测试5",targetEail, ccPeoples, bccPeoples,attachments,html,context,imageResources);
    }


}
